package ccnt.pvc.scudwaremobile.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import ccnt.pvc.scudwaremobile.dao.ServiceModel;
import ccnt.pvc.scudwaremobile.storage.ServiceDbManager;
import ccnt.pvc.scudwaremobile.utils.Constants.PreferenceKeys;
import ccnt.pvc.scudwaremobile.utils.Constants.PreferenceNames;
import ccnt.pvc.scudwaremobile.utils.FileUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class ServiceManager extends Service {

	private final static String TAG = ServiceManager.class.getName();
	
	ServiceDbManager serviceDbManager = null;
	
	@Override
	public void onCreate() {
		Log.d(TAG, "onCreate: ServiceManager");
		super.onCreate();
		
		serviceDbManager = new ServiceDbManager(ServiceManager.this);
		firstTimeInit();
	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy: ServiceManager");
		super.onDestroy();
	}
	
	public class ServiceManagerBinder extends Binder {
		public ServiceManager getService() {
			return ServiceManager.this;
		}
	}
	
	private final ServiceManagerBinder binder = new ServiceManagerBinder();
	
	@Override
	public IBinder onBind(Intent intent) {
		Log.d(TAG, "onBind: " + TAG);
		return binder;
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		Log.d(TAG, "onUnbind: " + TAG);
		return super.onUnbind(intent);
	}
	
	/**
	 * 第一次打开应用时，进行初始化
	 */
	private void firstTimeInit() {
		SharedPreferences preference = getSharedPreferences(
				PreferenceNames.LOGIN_PREFERENCE, MODE_PRIVATE);
		boolean isFirst = preference.getBoolean(PreferenceKeys.IS_FIRST, true);
		if(isFirst) {
			Log.d(TAG, "fisrt start");
			initServiceDb();
			preference.edit().putBoolean(PreferenceKeys.IS_FIRST, false)
					.commit();
		}
	}
	
	/**
	 * 向service_reg数据库存入内置的服务信息
	 */
	private void initServiceDb() {
		Map<String, ServiceModel> serviceMap = readDefaultServices();
		for(ServiceModel service : serviceMap.values()) {
			serviceDbManager.insertServiceToReg(service);
		}
	}
	
	/**
	 * 读取内置的service信息
	 * @return 包含service信息的map
	 */
	private Map<String, ServiceModel> readDefaultServices() {
		Map<String, ServiceModel> serviceMap = new HashMap<String, ServiceModel>();
		String jsonBuiltins = FileUtils.getFromAssets(ServiceManager.this, "builtin_services.json");
		JsonParser jsonParser = new JsonParser();
		JsonObject jsonObject = (JsonObject)jsonParser.parse(jsonBuiltins);
		JsonArray servicesArray = jsonObject.getAsJsonArray("services");
		Gson gson = new Gson();
		for(int i=0; i<servicesArray.size(); i++) {
			ServiceModel service = gson.fromJson(servicesArray.get(i), ServiceModel.class);
			serviceMap.put(service.getClassName(), service);
		}
		return serviceMap;
	}
	
	/**
	 * 通过类名获得服务信息
	 * @param className 类名
	 * @return 服务实例
	 */
	public ServiceModel getServiceByClassName(String className) {
		return serviceDbManager.getRegServiceByClassName(className);
	}
	
	public List<ServiceModel> getServices(List<ServiceModel> filters) {
		List<ServiceModel> services = new ArrayList<ServiceModel>();
		// TODO 根据条件过滤service
		services = serviceDbManager.getAllRegServices();
		
		return services;
	}
	
	public Map<String, ServiceModel> getServiceMap(List<ServiceModel> filters) {
		Map<String, ServiceModel> serviceMap = new HashMap<String, ServiceModel>();
		
		List<ServiceModel> services = serviceDbManager.getAllRegServices();
		for(ServiceModel service : services) {
			serviceMap.put(service.getClassName(), service);
		}
		return serviceMap;
	}

}
